package com.hn.gege.sys.modules.tt.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hn.gege.core.common.CommonPage;
import com.hn.gege.core.common.CommonResult;
import com.hn.gege.core.common.BaseController;
import com.hn.gege.core.query.QueryGenerator;
import com.hn.gege.sys.modules.tt.dto.TtExcelDTO;
import com.hn.gege.sys.modules.tt.entity.Tt;
import com.hn.gege.sys.modules.tt.service.ITtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;


@Api(tags = "tt-TtController", description = "注释管理")
@RestController
@RequestMapping("/tt/tt")
@Validated
public class TtController extends BaseController {

    @Autowired
    ITtService ttService;

    @ApiOperation("注释分页列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNo", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/pageInfo")
    public CommonPage<Tt> pageInfo(@NotNull Integer pageNo,
                                   @NotNull Integer pageSize,
                                   Tt tt,
                                   HttpServletRequest request){
        Page<Tt> pageInfo = new Page<>(pageNo, pageSize);
        QueryWrapper<Tt> qw = QueryGenerator.initQueryWrapper(tt, request.getParameterMap());
        Page<Tt> page1 = ttService.page(pageInfo, qw);
        return CommonPage.restPage(page1);
    }

    @ApiOperation("通过主键获取注释")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "uName", value = "姓名", required = true, dataType = "String", paramType = "path"),
		@ApiImplicitParam(name = "sex", value = "性别", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/{uName}/{sex}")
    public CommonResult<Tt> getByPk(@NotBlank@PathVariable("uName") String uName,
						@NotBlank@PathVariable("sex") String sex){
        Tt tt = ttService.getByPk(uName, sex);
        return CommonResult.success(tt);
    }

    @ApiOperation("新增注释")
    @PostMapping
    public CommonResult add(@Validated Tt tt, BindingResult bindingResult){
    boolean b = ttService.save(tt);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("修改注释")
    @PutMapping
    public CommonResult updateByPk(@Validated Tt tt, BindingResult bindingResult){
        boolean b = ttService.updateByPk(tt);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("通过主键删除注释")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "uName", value = "姓名", required = true, dataType = "String", paramType = "path"),
		@ApiImplicitParam(name = "sex", value = "性别", required = true, dataType = "String", paramType = "path")
    })
    @DeleteMapping("/{uName}/{sex}")
    public CommonResult removeByPk(@NotBlank@PathVariable("uName") String uName,
						@NotBlank@PathVariable("sex") String sex){
        boolean b = ttService.removeByPk(uName, sex);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("导出测试")
    @GetMapping("/expert")
    public CommonResult expert(){
        List<Tt> list = ttService.list();
        List<TtExcelDTO> datas = list.stream().map(item -> new TtExcelDTO().convertFor(item)).collect(Collectors.toList());
        // 写法1
        String fileName = "D:/file/" + UUID.randomUUID() + ".xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        EasyExcel.write(fileName, TtExcelDTO.class).sheet("模板").doWrite(datas);

        return CommonResult.success("成功");
    }

}
